var Prize = require('../models/Prize');

exports.award = function(user,callback){
	console.log(user);

	Prize.find({code:'一等奖',state:1})
		.limit(10)
		.exec(function(err,prizes){
		if(err){
			callback('错误','')
		}else{
			console.log(prizes)
			if(prizes.length){
				prizes[0].state = 2;
				prizes[0].user_id = user;
				prizes[0].save(function(err,_prize){
					if(err){
					callback('错误','')
					}else{
						callback('',_prize)
					}
				})
			}else{
				Prize.find({code:'二等奖',state:1})
					.limit(10)
					.exec(function(err,$prizes){
					if(err){
						callback('错误','')
					}else{
						if($prizes.length){
							$prizes[0].state = 2;
							$prizes[0].user_id = user;
							$prizes[0].save(function(err,_prize){
								if(err){
								callback('错误','')
								}else{
									callback('',_prize)
								}
							})
						}else{
							Prize.find({code:'三等奖',state:1})
								.limit(10)
								.exec(function(err,$$prizes){
								if(err){
									callback('错误','')
								}else{
									if($$prizes.length){
										$$prizes[0].state = 2;
										$$prizes[0].user_id = user;
										$$prizes[0].save(function(err,_prize){
											if(err){
											callback('错误','')
											}else{
												callback('',_prize)
											}
										})
									}else{
										callback('','')
									}
								}
							})
						}
					}
				})
			}
		}
	})
}